package migrate

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/define"
	"sale-admin/internal/models"
)

func (MigrateFunc) Script9() error {
	mysql.NewDB().Migrator().AddColumn(&models.MaActivityData{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaActivityData{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaAIServiceFaceCount{}, "created_at")
	mysql.NewDB().Migrator().AddColumn(&models.MaAIServiceFaceCount{}, "new_start_time")
	mysql.NewDB().Migrator().AddColumn(&models.MaAIServiceFaceCount{}, "new_end_time")
	mysql.NewDB().Exec("update " + models.MaAIServiceFaceCount{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	mysql.NewDB().Exec("update " + models.MaAIServiceFaceCount{}.TableName() + " set new_start_time = UNIX_TIMESTAMP(`start_time`)")
	mysql.NewDB().Exec("update " + models.MaAIServiceFaceCount{}.TableName() + " set new_end_time = UNIX_TIMESTAMP(`end_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaCloseContainnerLog{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaCloseContainnerLog{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaEmail{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaEmail{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaEmergency{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaEmergency{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaEventQuestion{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaEventQuestion{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaEventType{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaEventType{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaEvent{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaEvent{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsClass{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaGoodsClass{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsClick{}, "created_at")
	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsClick{}, "new_start_time")
	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsClick{}, "new_end_time")
	mysql.NewDB().Exec("update " + models.MaGoodsClick{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	mysql.NewDB().Exec("update " + models.MaGoodsClick{}.TableName() + " set new_start_time = UNIX_TIMESTAMP(`start_time`)")
	mysql.NewDB().Exec("update " + models.MaGoodsClick{}.TableName() + " set new_end_time = UNIX_TIMESTAMP(`end_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaGoodsMulti{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaGoodsMulti{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaOrderDetails{}, "created_at")
	mysql.NewDB().Migrator().AddColumn(&models.MaOrderDetails{}, "updated_at")
	mysql.NewDB().Exec("update " + models.MaOrderDetails{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	mysql.NewDB().Exec("update " + models.MaOrderDetails{}.TableName() + " set updated_at = UNIX_TIMESTAMP(`modify_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaOrderNotify{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaOrderNotify{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaOrderRefund{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaOrderRefund{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "created_at")
	mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "updated_at")
	mysql.NewDB().Exec("update " + models.MaOrder{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	mysql.NewDB().Exec("update " + models.MaOrder{}.TableName() + " set updated_at = UNIX_TIMESTAMP(`modify_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaPayment{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaPayment{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaPersonInfoBama{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaPersonInfoBama{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaPersonInfoLook{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaPersonInfoLook{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaPhysicalInfo{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaPhysicalInfo{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionCode{}, "created_at")
	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionCode{}, "new_expiration_time")
	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionCode{}, "new_usage_time")
	mysql.NewDB().Exec("update " + models.MaRedemptionCode{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	mysql.NewDB().Exec("update " + models.MaRedemptionCode{}.TableName() + " set new_expiration_time = UNIX_TIMESTAMP(`expiration_time`)")
	mysql.NewDB().Exec("update " + models.MaRedemptionCode{}.TableName() + " set new_usage_time = UNIX_TIMESTAMP(`usage_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionLog{}, "created_at")
	mysql.NewDB().Migrator().AddColumn(&models.MaRedemptionLog{}, "new_expiration_time")
	mysql.NewDB().Exec("update " + models.MaRedemptionLog{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	mysql.NewDB().Exec("update " + models.MaRedemptionLog{}.TableName() + " set new_expiration_time = UNIX_TIMESTAMP(`expiration_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobotBackApk{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaRobotBackApk{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobotConfig{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaRobotConfig{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobotErrorLogPallets{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaRobotErrorLogPallets{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobotErrorLog{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaRobotErrorLog{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobotReportLog{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaRobotReportLog{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobotSalesInfo{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaRobotSalesInfo{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobotUpdateApk{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaRobotUpdateApk{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaRobot{}, "created_at")
	mysql.NewDB().Migrator().AddColumn(&models.MaRobot{}, "new_last_reported_time")
	mysql.NewDB().Exec("update " + models.MaRobot{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	mysql.NewDB().Exec("update " + models.MaRobot{}.TableName() + " set new_last_reported_time = UNIX_TIMESTAMP(`last_reported_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaSaleInfoLogDetail{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaSaleInfoLogDetail{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaSaleInfoLog{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaSaleInfoLog{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaWechatRate{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaWechatRate{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")

	mysql.NewDB().Migrator().AddColumn(&models.MaWechatUser{}, "created_at")
	mysql.NewDB().Exec("update " + models.MaWechatUser{}.TableName() + " set created_at = UNIX_TIMESTAMP(`add_time`)")
	return nil
}

func (MigrateFunc) Script10() error {
	mysql.NewDB().Exec("update `ma_goods_info` set class_id = 0 where class_id is null")
	mysql.NewDB().Exec("alter table `ma_goods_info` modify column `class_id` int NOT NULL DEFAULT 0 COMMENT '类目ID'")
	return nil
}

func (MigrateFunc) Script11() error {
	mysql.NewDB().Exec("alter table `ma_robot` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	return nil
}

func (MigrateFunc) Script12() error {
	mysql.NewDB().Exec("alter table `ma_ai_service_face_count` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	mysql.NewDB().Exec("alter table `ma_goods_class` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	mysql.NewDB().Exec("alter table `ma_goods_click` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	mysql.NewDB().Exec("alter table `ma_goods_info` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	mysql.NewDB().Exec("alter table `ma_redemption_code` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	mysql.NewDB().Exec("alter table `ma_order` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	mysql.NewDB().Exec("alter table `ma_order_notify` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	mysql.NewDB().Exec("alter table `ma_redemption_log` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	return nil
}

func (MigrateFunc) Script13() error {
	// mysql.NewDB().Migrator().AddColumn(&models.MaPaymentWechat{}, "cert_file")
	return nil
}

func (MigrateFunc) Script14() error {
	mysql.NewDB().Exec("alter table `ma_order_refund` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	return nil
}

func (MigrateFunc) Script15() error {
	mysql.NewDB().Exec("alter table `ma_robot_error_log` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	return nil
}

func (MigrateFunc) Script16() error {
	// mysql.NewDB().Migrator().AutoMigrate(&models.MaPaymentStripe{})
	return nil
}

func (MigrateFunc) Script17() error {
	mysql.NewDB().Exec("alter table `ma_robot_error_log_pallets` modify column `id` bigint unsigned NOT NULL COMMENT '商户ID'")
	return nil
}

func (MigrateFunc) Script18() error {
	if !mysql.NewDB().Migrator().HasColumn(&models.MaGoodsInfo{}, "external_product_id") {
		mysql.NewDB().Migrator().AddColumn(&models.MaGoodsInfo{}, "external_product_id")
	}
	return nil
}

func (MigrateFunc) Script19() error {
	mysql.NewDB().Migrator().AutoMigrate(&models.MaGoodsActivity{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaGoodsActivityDetail{})
	return nil
}

func (MigrateFunc) Script20() error {
	mysql.NewDB().Exec("alter table `ma_payment_stripe` modify column `stripe_priv_key` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT 'stripe私钥'")
	return nil
}

// 新增：加购子订单字段,删除主订单唯一索引
func (MigrateFunc) Script21() error {
	mysql.NewDB().Exec("alter table `ma_order` add column `child_out_trade_no` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '子订单号'")
	return nil
}

// 新增：加购商品语音属性
func (MigrateFunc) Script22() error {
	mysql.NewDB().Exec("alter table `ma_goods_info` add column `goods_voice` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci COMMENT '商品语音属性'")
	mysql.NewDB().Exec("alter table `ma_order` drop index `OutTradeNo`")
	mysql.NewDB().Exec("alter table `ma_order` add unique index OutTradeNoIndex(`out_trade_no`,`child_out_trade_no`)")
	return nil
}

// 新增：加购优惠功能，订单新增优惠金额字段
func (MigrateFunc) Script23() error {
	mysql.NewDB().Exec("alter table `ma_order` add column `discount_amount` decimal(10,2) default '0' COMMENT '优惠金额'")
	mysql.NewDB().Exec("ALTER TABLE `ma_physical_info` ADD INDEX `idx_salesid_addtime_msg` (`sales_id`, `add_time`, `msg`)")
	// 人物画像数据优化迁移
	db := mysql.NewDB()
	db.AutoMigrate(&models.MaPhysicalInfo{})
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 1 AND age<=4").Update("age_range", 4)
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 5 AND age<=7").Update("age_range", 7)
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 8 AND age<=11").Update("age_range", 11)
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 12 AND age<=19").Update("age_range", 19)
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 20 AND age<=29").Update("age_range", 29)
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 30 AND age<=39").Update("age_range", 39)
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 40 AND age<=49").Update("age_range", 49)
	db.Model(&models.MaPhysicalInfo{}).Where("age >= 50").Update("age_range", 50)
	db.Model(&models.MaPhysicalInfo{}).Where("age = -1").Update("age_range", 0)
	//订单表增加areas5
	if !mysql.NewDB().Migrator().HasColumn(&models.MaOrder{}, "areas5") {
		mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "areas5")
	} else if mysql.NewDB().Migrator().HasColumn(&models.MaOrder{}, "Areas5") {
		db.Exec("alter table `ma_order` change column `Areas5` `areas5` bigint(20) DEFAULT NULL")
	}
	mysql.NewDB().Exec("ALTER TABLE `ma_goods_click` ADD INDEX `end_time` (`end_time`)")
	return nil
}

// 修复曝光量未保存areas3的问题
func (MigrateFunc) Script24() error {
	mysql.NewDB().Exec("update ma_ai_service_face_count ai left join ma_robot as robot on robot.robot_name = ai.robot_name set ai.areas3 = robot.areas3 where ai.areas3 is null")
	return nil
}

// 修复订单表的支付方式默认值问题
func (MigrateFunc) Script25() error {
	mysql.NewDB().Exec("update ma_order set payment_id = 0 where payment_id is null")
	mysql.NewDB().Exec("alter table `ma_order` modify column `payment_id` tinyint NOT NULL DEFAULT 0 COMMENT '1-微信 2-支付宝 3支付宝人脸；\r\n3-刷卡\r\n4-好邻居 5 - 兑换码 6 大兴支付 7 赠送'")
	return nil
}

func (MigrateFunc) Script26() error {
	// 修复微信用户表的昵称字段类型问题
	mysql.NewDB().Exec("alter table `ma_wechat_user` modify column `nick_name` varchar(255) NOT NULL default '' COMMENT '微信昵称'")
	if !mysql.NewDB().Migrator().HasColumn(&models.MaWechatUser{}, "phone") {
		mysql.NewDB().Migrator().AddColumn(&models.MaWechatUser{}, "phone")
	}
	return nil
}

func (MigrateFunc) Script27() error {
	// 新增：运营报告2个字段，WebHookName,WebHookAddress
	mysql.NewDB().Exec("alter table `ma_robot_config` add column `web_hook_name` varchar(255) NOT NULL default '' COMMENT '推送机器人名称'")
	mysql.NewDB().Exec("alter table `ma_robot_config` add column `web_hook_address` varchar(255) NOT NULL default '' COMMENT '推送机器人webhook地址'")

	// 新增2个字段：运营日报当天的机器的库存和最大库存
	mysql.NewDB().Exec("alter table `ma_robot_report` add column `goods_max_inventory` int(10) NOT NULL default 0 COMMENT '机器允许最大库存'")
	mysql.NewDB().Exec("alter table `ma_robot_report` add column `goods_inventory` int(10) NOT NULL default 0 COMMENT '机器当前库存'")
	mysql.NewDB().Exec("alter table `ma_robot_report` add column `areas3_name` varchar(120) NOT NULL COMMENT '商场中文名称'")
	// 报告记录表，新增报告名称和推送机器人名称、推送地址
	mysql.NewDB().Exec("alter table `ma_robot_report_log` add column `object_name` varchar(120) NOT NULL COMMENT '报告名称'")
	mysql.NewDB().Exec("alter table `ma_robot_report_log` add column `web_hook_name` varchar(120) NOT NULL COMMENT '推送名称'")
	mysql.NewDB().Exec("alter table `ma_robot_report_log` add column `web_hook_url` varchar(120) NOT NULL COMMENT '推送地址'")
	mysql.NewDB().Exec("alter table `ma_robot_report_log` add column `merchant_id` varchar(255) NOT NULL default '' COMMENT '商户ID'")
	if !mysql.NewDB().Migrator().HasColumn(&models.MaGoodsInfo{}, "points") {
		mysql.NewDB().Migrator().AddColumn(&models.MaGoodsInfo{}, "points")
	}
	if !mysql.NewDB().Migrator().HasColumn(&models.MaGoodsInfo{}, "points_support") {
		mysql.NewDB().Migrator().AddColumn(&models.MaGoodsInfo{}, "points_support")
	}
	db := mysql.NewDB()
	db.Migrator().AutoMigrate(&models.MaRobotInventory{})             // 历史库存
	db.Migrator().AutoMigrate(&models.MaRobotReportZeroStockLog{})    // 连续空货和单日空货生成的报告记录表
	db.Migrator().AutoMigrate(&models.MaRobotInventoryZeroSingel{})   // 单日空货数据
	db.Migrator().AutoMigrate(&models.MaRobotInventoryZeroContinue{}) // 连续空货数据
	if !mysql.NewDB().Migrator().HasColumn(&models.MaOrder{}, "actual_amount") {
		mysql.NewDB().Migrator().AddColumn(&models.MaOrder{}, "actual_amount")
		mysql.NewDB().Exec("update ma_order set actual_amount = payment_amount where actual_amount = 0.00")
	}
	mysql.NewDB().AutoMigrate(&models.MaOrderPoints{})
	mysql.NewDB().AutoMigrate(&models.MaOrderRefund{})
	mysql.NewDB().AutoMigrate(&models.MaRobotConfig{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaRobot{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaRobotReportLog{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaSalesStatement{})
	mysql.NewDB().Migrator().AutoMigrate(&models.MaRobotReport{})
	return nil
}

func (MigrateFunc) Script28() error {
	mysql.NewDB().Exec("ALTER TABLE `ma_wechat_user` MODIFY COLUMN `nick_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '微信昵称' AFTER `id`")
	mysql.NewDB().Migrator().AutoMigrate(&models.MaGoodsInfo{})
	// 重新设置auto_increment的当前记录
	if define.APPVersion == "v3.2.608" {
		mysql.NewDB().Exec("ALTER TABLE ma_goods_info AUTO_INCREMENT = 1000")
	}
	return nil
}

func (MigrateFunc) Script29() error {
	mysql.NewDB().Migrator().AddColumn(&models.MaOrderDetails{}, "stripe_ex_create_result")
	mysql.NewDB().Exec("ALTER TABLE `ma_sales_statement` MODIFY COLUMN `content` text NOT NULL COMMENT '提示内容'")
	// 添加订单表，子订单和确认订单号新增索引
	mysql.NewDB().Exec("ALTER TABLE `ma_order` ADD INDEX `idx_end_out_trade_no` (`end_out_trade_no`)")
	mysql.NewDB().Exec("ALTER TABLE `ma_order` ADD INDEX `idx_child_out_trade_no` (`child_out_trade_no`)")
	return nil
}

func (MigrateFunc) Script30() error {
	// mysql.NewDB().Migrator().AutoMigrate(&models.MaPaymentSmall{})
	return nil
}

func (MigrateFunc) Script31() error {
	mysql.NewDB().Exec("alter table `ma_order_refund` add column `refund_user_id` varchar(30) default '' COMMENT '退款人ID'")
	return nil
}
